﻿using System.Collections.Generic;
using Microsoft.SharePoint;
using SharePoint.Orm.Metal.Rules;

namespace SharePoint.Orm.Metal {
	public class CodeGeneratorConfiguration {
		private readonly List<ListRule> _listRules = new List<ListRule>();

		public CodeGeneratorConfiguration() {
			DataContextNamespaceName = "SPGenerated";
			ContentTypeNamespaceName = "SPGenerated.ContentTypes";
			ListNamespaceName = "SPGenerated.Lists";
			DataContextClassName = "MySPDataContext";
			ListFileNameSuffix = ".orm";
			ContentTypeFileNameSuffix = ".orm";
			DataContextFileNameSuffix = ".orm";
			DataContextOutputDirectory = "./";
			ListOutputDirectory = "./Lists";
			ContentTypeOutputDirectory = "./ContentTypes";
			NameRewriter = new DefaultNameRewriter();
			FieldTypeResolver = new DefaultFieldTypeResolver();
			ContentTypeTemplate = "res://SharePoint.Orm.Metal.Templates.Basic.ContentType.tt, SharePoint.Orm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5959a709b4f356a5";
			DataContextTemplate = "res://SharePoint.Orm.Metal.Templates.Basic.DataContext.tt, SharePoint.Orm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5959a709b4f356a5";
			ListTemplate = "res://SharePoint.Orm.Metal.Templates.Basic.List.tt, SharePoint.Orm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5959a709b4f356a5";
		}

		public string ContentTypeTemplate { get; set; }
		public string DataContextTemplate { get; set; }
		public string ListTemplate { get; set; }
		public string Url { get; set; }
		public string DataContextNamespaceName { get; set; }
		public string DataContextClassName { get; set; }
		public string ContentTypeNamespaceName { get; set; }
		public string ListNamespaceName { get; set; }
		public string ListFileNameSuffix { get; set; }
		public string ListOutputDirectory { get; set; }
		public string ContentTypeOutputDirectory { get; set; }
		public string DataContextOutputDirectory { get; set; }
		public string DataContextFileNameSuffix { get; set; }
		public string ContentTypeFileNameSuffix { get; set; }
		public INameRewriter NameRewriter { get; set; }
		public IFieldTypeResolver FieldTypeResolver { get; set; }

		public void AddListRule(ListRule listRule) {
			_listRules.Add(listRule);
		}

		public bool IncludeList(SPList list) {
			foreach (ListRule listRule in _listRules) {
				if (listRule.Match(list)) {
					if (listRule is ListExcludeRule) { // Change to a greedy exclude
					    return false;
					}
					if(listRule is ListIncludeRule)
						return true;
				}
			}
			return false;
		}
	}
}
